package com.yungu.swift.api.base.filter;

import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author cuixiuyin
 * 对表情进行过滤
 */
@WebFilter(urlPatterns = {"/api/*", "/h5/*"})
public class EmojiFilter extends OncePerRequestFilter {

    private static Pattern NUMBER_EMOJI_PATTERN = Pattern.compile("(?:[\uD83C\uDF00-\uD83D\uDDFF]" +
            "|[\uD83E\uDD00-\uD83E\uDDFF]" +
            "|[\uD83D\uDE00-\uD83D\uDE4F]" +
            "|[\uD83D\uDE80-\uD83D\uDEFF]" +
            "|[\u2600-\u26FF]\uFE0F?" +
            "|[\u2700-\u27BF]\uFE0F?" +
            "|\u24C2\uFE0F?" +
            "|[\uD83C\uDDE6-\uD83C\uDDFF]{1,2}" +
            "|[\uD83C\uDD70\uD83C\uDD71\uD83C\uDD7E\uD83C\uDD7F\uD83C\uDD8E\uD83C\uDD91-\uD83C\uDD9A]\uFE0F?" +
            "|[\u0023\u002A\u0030-\u0039]\uFE0F?\u20E3" +
            "|[\u2194-\u2199\u21A9-\u21AA]\uFE0F?" +
            "|[\u2B05-\u2B07\u2B1B\u2B1C\u2B50\u2B55]\uFE0F?" +
            "|[\u2934\u2935]\uFE0F?" +
            "|[\u3030\u303D]\uFE0F?" +
            "|[\u3297\u3299]\uFE0F?" +
            "|[\uD83C\uDE01\uD83C\uDE02\uD83C\uDE1A\uD83C\uDE2F\uD83C\uDE32-\uD83C\uDE3A\uD83C\uDE50\uD83C\uDE51]\uFE0F?" +
            "|[\u203C\u2049]\uFE0F?" +
            "|[\u25AA\u25AB\u25B6\u25C0\u25FB-\u25FE]\uFE0F?" +
            "|[\u00A9\u00AE]\uFE0F?" +
            "|[\u2122\u2139]\uFE0F?" +
            "|\uD83C\uDC04\uFE0F?" +
            "|\uD83C\uDCCF\uFE0F?" +
            "|[\u231A\u231B\u2328\u23CF\u23E9-\u23F3\u23F8-\u23FA]\uFE0F?)");

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        Map<String, String[]> requestMap = request.getParameterMap();
        ChangeRequestWrapper changeRequestWrapper = new ChangeRequestWrapper(request);
        HashMap<String, String[]> changeRequestMap = new HashMap<>(changeRequestWrapper.getParameterMap());
        Iterator<Map.Entry<String, String[]>> iterator = requestMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String[]> next = iterator.next();
            //过滤emoji
            Matcher matcher = NUMBER_EMOJI_PATTERN.matcher(next.getValue()[0]);
            //判断是否含有emoji
            if (matcher.find()) {
                String emojiCode = URLEncoder.encode(matcher.group(0), "UTF-8");
                String newValue = matcher.replaceAll(emojiCode);
                changeRequestMap.put(next.getKey(), new String[]{newValue});
                //重写request
                changeRequestWrapper.setParameterMap(changeRequestMap);
            }
        }
        filterChain.doFilter(changeRequestWrapper, response);
    }

}
